package com.fancy.recommendation.controller;

import com.fancy.common.dto.response.Result;
import com.fancy.recommendation.dto.request.RankingRequestDTO;
import com.fancy.recommendation.dto.response.RankingResponseVO;
import com.fancy.recommendation.service.RankingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 榜单控制器
 *
 * @author fancy
 */
@Api(tags = "榜单服务")
@Slf4j
@RestController
@RequestMapping("/api/v1/rankings")
@RequiredArgsConstructor
public class RankingController {

    private final RankingService rankingService;

    @ApiOperation(value = "获取热门榜单", notes = "获取指定类型的热门榜单")
    @GetMapping("/{rankingType}")
    public Result<RankingResponseVO> getRanking(
            @ApiParam(value = "榜单类型", required = true) @PathVariable String rankingType,
            @ApiParam(value = "周期类型") @RequestParam(defaultValue = "weekly") String period,
            @ApiParam(value = "榜单条数") @RequestParam(defaultValue = "50") Integer limit,
            @ApiParam(value = "指定日期") @RequestParam(required = false) String date) {

        try {
            RankingRequestDTO request = new RankingRequestDTO();
            request.setRankingType(rankingType);
            request.setPeriodType(period);
            request.setLimit(limit);
            request.setDate(date);

            RankingResponseVO ranking = rankingService.getRanking(request);
            return Result.success(ranking);
        } catch (Exception e) {
            log.error("获取榜单失败: rankingType={}, period={}", rankingType, period, e);
            return Result.fail("获取榜单失败");
        }
    }

    @ApiOperation(value = "获取榜单类型列表", notes = "获取所有可用的榜单类型")
    @GetMapping
    public Result<List<Map<String, Object>>> getRankingTypes() {
        try {
            List<Map<String, Object>> types = rankingService.getRankingTypes();
            return Result.success(types);
        } catch (Exception e) {
            log.error("获取榜单类型失败", e);
            return Result.fail("获取榜单类型失败");
        }
    }

    @ApiOperation(value = "获取榜单历史", notes = "获取指定榜单的历史数据")
    @GetMapping("/{rankingType}/history")
    public Result<List<RankingResponseVO>> getRankingHistory(
            @ApiParam(value = "榜单类型", required = true) @PathVariable String rankingType,
            @ApiParam(value = "周期类型") @RequestParam(defaultValue = "weekly") String period,
            @ApiParam(value = "历史期数") @RequestParam(defaultValue = "10") Integer limit) {

        try {
            List<RankingResponseVO> history = rankingService.getRankingHistory(rankingType, period, limit);
            return Result.success(history);
        } catch (Exception e) {
            log.error("获取榜单历史失败: rankingType={}, period={}", rankingType, period, e);
            return Result.fail("获取榜单历史失败");
        }
    }

    @ApiOperation(value = "获取实时热门", notes = "获取实时热门内容")
    @GetMapping("/realtime/{contentType}")
    public Result<List<Map<String, Object>>> getRealtimeHotContent(
            @ApiParam(value = "内容类型", required = true) @PathVariable String contentType,
            @ApiParam(value = "数量限制") @RequestParam(defaultValue = "20") Integer limit) {

        try {
            List<Map<String, Object>> hotContent = rankingService.getRealtimeHotContent(contentType, limit);
            return Result.success(hotContent);
        } catch (Exception e) {
            log.error("获取实时热门失败: contentType={}", contentType, e);
            return Result.fail("获取实时热门失败");
        }
    }

    @ApiOperation(value = "获取榜单统计", notes = "获取榜单的统计信息")
    @GetMapping("/{rankingType}/stats")
    public Result<Map<String, Object>> getRankingStatistics(
            @ApiParam(value = "榜单类型", required = true) @PathVariable String rankingType,
            @ApiParam(value = "周期类型") @RequestParam(defaultValue = "weekly") String period) {

        try {
            Map<String, Object> stats = rankingService.getRankingStatistics(rankingType, period);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取榜单统计失败: rankingType={}, period={}", rankingType, period, e);
            return Result.fail("获取榜单统计失败");
        }
    }
}
